package com.zl.smartmall.library.account.po;

import org.apache.http.Header;

import com.zl.smartmall.library.util.CrashHandler;

import android.text.TextUtils;

import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Property;
import net.tsz.afinal.annotation.sqlite.Table;
import net.tsz.afinal.annotation.sqlite.Transient;

@Table(name = "tb_CookieInfo")
public class CookieInfo {

	@Transient
	private static final String TAG = CookieInfo.class.getSimpleName();
	@Id
	private int id;
	@Property
	private String cookieValue;
	@Property
	private int uid;
	@Property
	private int loginType;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCookieValue() {
		return cookieValue;
	}

	public void setCookieValue(String cookieValue) {
		this.cookieValue = cookieValue;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public int getLoginType() {
		return loginType;
	}

	public void setLoginType(int loginType) {
		this.loginType = loginType;
	}
	
	public static CookieInfo parse(Header[] headers, int uid, int loginType) {
		for (Header header : headers) {
			if (header.getName().equalsIgnoreCase("Set-Cookie")) {
				String value = header.getValue();
				String[] split = TextUtils.split(value, ";");
				for (String s : split) {
					if (s.trim().startsWith("access_token")) {
						CookieInfo info = new CookieInfo();
						String cookieValue = TextUtils.split(s, "=")[1];
						info.cookieValue = cookieValue;
						info.loginType = loginType;
						info.uid = uid;
						
						CrashHandler.d(TAG, "cookie值 = " + cookieValue);
						
						return info;
					}
				}
			}
		}
		
		return null;
	}

}
